package com.ximmerse.chengdon;

import org.testng.annotations.Test;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import initAll.TestNGListener;
import initAll.eventsListener;
import initAll.getWebDriver;
import initAll.login;
import initAll.login2;
import javaUnit.ThreadTime;
import seleniumUnit.linkOperate;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import java.util.concurrent.TimeUnit;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import org.testng.ITestResult;
import org.testng.annotations.AfterClass;

public class homePageLogin2 {
	private static EventFiringWebDriver driver;
	private static String url = "http://10.168.1.250:9080/";
	private static login2 login;
	private static final Logger log = LogManager.getLogger(homePageLogin2.class.getName());
	ExtentReports report = TestNGListener.report;
	ExtentTest test = TestNGListener.test;
	linkOperate Operate;

	//用户登录-退出
	@Test(priority=0,description = "用户登录/退出")
	public void login_out() {
		test.log(Status.INFO, "当前URL="+driver.getCurrentUrl());
		//登录:输入用户名/密码
//		WebElement edit_UserName=driver.findElement(By.xpath("//div[@class='el-form-item__content'][1]/div[1]/input[@type='text']"));
//		edit_UserName.clear();
//		edit_UserName.sendKeys("admin");

		WebElement edit_UserName=driver.findElement(By.xpath("//div[@class='el-form-item__content'][1]/div[1]/input[@type='text']"));
		WebElement edit_Password=driver.findElement(By.xpath("//div[@class='el-form-item__content'][1]/div[1]/input[@type='password']"));
		WebElement btn_login=driver.findElement(By.xpath("//button[@class='el-button form-submit el-button--default el-button--large']/span"));
		edit_UserName.clear();
		edit_UserName.sendKeys("admin");
		test.log(Status.INFO, "	输入用户名");
		report.flush();
		edit_Password.clear();
		edit_Password.sendKeys("123456");
		test.log(Status.INFO, "	输入密码");
		

		//点击登陆
		btn_login.click();
		test.log(Status.INFO, "	点击登陆按钮");
		
		//等待登陆
		//WebElement userName=Operate.getClickWebElement(driver,"//div[2]/div/div/div/span[1]","xpath",10);
		ThreadTime.sleepTime(5);
		test.log(Status.INFO, "登陆成功，用户实名："+login.btn_userName.getText());
		test.log(Status.INFO, "当前URL="+driver.getCurrentUrl());
		//退出登陆
		login.btn_userName.click();
		test.log(Status.INFO, "点击用户实名");
		
		WebElement ele=Operate.getClickWebElement(driver,"//a[@href='javascript:' and contains(@class,'dropdown-btn')]/i[@class='icon fa fa-sign-out']","xpath",10);
		ThreadTime.sleepTime(1);
		ele.click();
		test.log(Status.INFO, "点击退出登陆");
		
		WebElement btn_determine=Operate.getClickWebElement(driver,"//div[@class='el-message-box__btns']/button[2]","xpath",10);
		ThreadTime.sleepTime(1);
		btn_determine.click();
		test.log(Status.INFO, "点击确认");
		long start=System.currentTimeMillis();
		while(!"http://10.168.1.250:9080/#/login".equals(driver.getCurrentUrl())){
			if("http://10.168.1.250:9080/#/login".equals(driver.getCurrentUrl())){
				test.log(Status.INFO, "退出成功");
				test.log(Status.INFO, "当前URL="+driver.getCurrentUrl());
				return;
			}
			if(ThreadTime.timeout(start,10))break;
			ThreadTime.sleepTime(1);
		}
		report.flush();
	}

	@BeforeMethod(description = "跳转到-首页")
	public void beforeMethod() {
//		ThreadTime.sleepTime(1);
//		driver.get(url);
//		ThreadTime.sleepTime(1);
	}

	@AfterMethod(description = "记录测试结果")
	public void afterMethod(ITestResult result) {
		String desc = result.getMethod().getDescription();
		String methodName = result.getMethod().getMethodName();
		switch (result.getStatus()) {
		case ITestResult.FAILURE:
			test.log(Status.FAIL, methodName + " " + desc +" --Run Result Fail");
			break;
		case ITestResult.SKIP:
			test.log(Status.SKIP, methodName + " " + desc + " --Run Result skip");
			break;
		case ITestResult.SUCCESS:
			test.log(Status.PASS, methodName + " " + desc + " --Run Result pass");
			break;
		}
		report.flush();
	}

	@BeforeClass(description = "初始化WebDriver")
	@Parameters({ "browser" })
	public void beforeClass(String browser) {
		test = report.createTest(browser + " Test");
		//driver = new EventFiringWebDriver(getWebDriver.setInternetIE(browser)).register(new eventsListener()).getWrappedDriver();
		driver = new EventFiringWebDriver(getWebDriver.setInternetIE(browser));
		driver.register(new eventsListener());
		test.log(Status.INFO, "start browser=" + browser);
		driver.get(url);
		test.log(Status.INFO, "get URL=" + url);

		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		driver.manage().window().maximize();
		test.log(Status.INFO, "browser set max...");
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("111111111111111111");
		WebElement edit_UserName=driver.findElement(By.xpath("//div[@class='el-form-item__content'][1]/div[1]/input[@type='text']"));
		WebElement edit_Password=driver.findElement(By.xpath("//div[@class='el-form-item__content'][1]/div[1]/input[@type='password']"));
		edit_UserName.clear();
		edit_UserName.sendKeys("admin");
		test.log(Status.INFO, "	输入用户名");
		report.flush();
		edit_Password.clear();
		edit_Password.sendKeys("123456");
		test.log(Status.INFO, "	输入密码");
		System.out.println("2222222222222222222222222222");
		//login = new login2(test, driver);
		//Operate = new linkOperate(test, driver);
		report.flush();
	}

	@AfterClass(description = "关闭浏览器")
	public void afterClass() {
		ThreadTime.sleepTime(2);
		test.log(Status.INFO, "Close IE");
		report.flush();
		ThreadTime.sleepTime(1);
		driver.quit();
	}
}
